const express = require('express');
const http = require('http');
const socketIO = require('socket.io');
const app = express();
const server = http.createServer(app);

const io = socketIO(server, {
  cors: {
    origin: '*',
    credential: true,
  }
});

io.on('connection', (socket) => {
  console.log('user connected');
  socket.on('hello', (data) => {
    console.log(`收到客户端的消息：${data}`);
    if (/头疼/.test(data)) {
      setTimeout(() => {
        socket.emit('answer', { type: 1, message: '吃点头疼药' })
      }, 1000);

    } else if (/发烧不咳嗽/.test(data)) {
      setTimeout(() => {
        socket.emit('answer', { type: 1, message: '这种情况考虑上呼吸道感染可能性比较大，可以口服连花清瘟胶囊先治疗三天看看' })
      }, 1000);

    }
    else {
      socket.broadcast.emit('doctor', { type: 1, message: data })
    }

  })
  let greet = {
    first: '您好，我是您的医学助理',
    second: `请详细描述您的问题，包括症状和持续时间
            如：发烧但不咳嗽，3天了`
  }
  socket.emit('sayHello', greet)

  socket.on('askDoctor', data => {
    console.log(data);
    socket.emit('realDoctor', { type: 1, message: '你好，我是王医生，请问有什么可以帮您的吗？' })
  })

});


server.listen(24000, () => {
  console.log("server is up and running on port 24000");
});